from google.appengine.ext import db

class AirportInfo(db.Model):
    airport_code = db.StringProperty(required=True)
    iata_code = db.StringProperty(required=False)
    icao_code = db.StringProperty(required=False)
    faa_code = db.StringProperty(required=False)
    active = db.BooleanProperty(required=True)
    city = db.StringProperty(required=True)
    country_code = db.StringProperty(required=True)
    is_major_airport = db.BooleanProperty(required=True)
    coords = db.GeoPtProperty(required=True)
    name = db.StringProperty(required=False)
    postal_code = db.StringProperty(required=False)
    state_code = db.StringProperty(required=False)
    street_1 = db.StringProperty(required=False)
    street_2 = db.StringProperty(required=False)
    weather_station_code = db.StringProperty(required=False)
    weather_zone = db.StringProperty(required=False)
    
    @classmethod
    def populate(cls, info_dict):
        return AirportInfo(key_name=info_dict.get('AirportCode'),
                    airport_code=info_dict.get('AirportCode'),
                    iata_code=info_dict.get('IATACode'),
                    icao_code=info_dict.get('ICAOCode'),
                    faa_code=info_dict.get('FAACode'),
                    active=info_dict.get('Active') == 'true',
                    city=info_dict.get('City'),
                    country_code=info_dict.get('CountryCode'),
                    is_major_airport=info_dict.get('IsMajorAirport') == 'true',
                    coords=db.GeoPt(info_dict.get('Latitude'), info_dict.get('Longitude')),
                    name=info_dict.get('Name'),
                    postal_code=info_dict.get('PostalCode'),
                    state_code=info_dict.get('StateCode'),
                    street_1=info_dict.get('Stree1'),
                    street_2=info_dict.get('Street2'),
                    weather_station_code=info_dict.get('WeatherStationCode'),
                    weather_zone=info_dict.get('WeatherZone'))
    
class AirlineInfo(db.Model):
    airline_code = db.StringProperty(required=True)
    iata_code = db.StringProperty(required=False)
    icao_code = db.StringProperty(required=False)
    name = db.StringProperty(required=True)
    
    @classmethod
    def populate(cls, info_dict):
        return AirlineInfo(key_name=info_dict.get('AirlineCode'),
                    airline_code=info_dict.get('AirlineCode'),
                    iata_code=info_dict.get('IATACode'),
                    icao_code=info_dict.get('ICAOCode'),
                    name=info_dict.get('Name'))
   
class DepartureFlightInfo(db.Model):
    departure_date = db.DateTimeProperty(required=True)
    departure_airport_iata_code = db.StringProperty(required=True)
    departure_timezone_offset = db.IntegerProperty(required=True)
    arrival_date = db.DateTimeProperty(required=True)
    arrival_airport_iata_code = db.StringProperty(required=True)
    arrival_timezone_offset = db.IntegerProperty(required=True)
    airline_code = db.StringProperty(required=True)
    flight_number = db.StringProperty(required=True)
    aircraft_type = db.StringProperty(required=False)
    
def get_departure_flight_info_db(departure_airport_iata_code, departure_date):
    departures = db.GqlQuery("select * from DepartureFlightInfo where departure_date >= DATETIME('" + departure_date + " 00:00:00') and departure_date <= DATETIME('" + departure_date + " 23:59:59') and departure_airport_iata_code = '" + departure_airport_iata_code + "' order by departure_date asc")
    departures = list(departures)
    return departures
        
def get_airport_info_db(country_code=None):
    country_code_condition = ""
    if country_code != None:
        country_code_condition = " and country_code = '" + country_code + "'"
    
    airports = db.GqlQuery("select * from AirportInfo where active=true" + country_code_condition + " order by iata_code desc")
    airports = list(airports)
    
    airport_dict = {}
    for airport_info in airports:
        entry = {}
        entry['AirportCode'] = airport_info.airport_code
        entry['Name'] = airport_info.name
        entry['City'] = airport_info.city
        entry['CountryCode'] = airport_info.country_code
        airport_dict[airport_info.iata_code] = entry
    return airport_dict


